package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.FlashPromotionProductRelation;
import com.itheima.domain.FlashPromotionSession;
import com.itheima.mapper.FlashPromotionProductRelationMapper;
import com.itheima.mapper.FlashPromotionSessionMapper;
import com.itheima.service.SmsFlashPromotionSessionService;
import com.itheima.vo.SmsFlashPromotionSessionVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class SmsFlashPromotionSessionServiceImpl implements SmsFlashPromotionSessionService {
    @Autowired
    FlashPromotionSessionMapper sessionMapper;
    @Autowired
    FlashPromotionProductRelationMapper relationMapper;

    //获取全部场次
    @Override
    public List<FlashPromotionSession> list() {

        LambdaQueryWrapper<FlashPromotionSession> lqw = new LambdaQueryWrapper<>();
        lqw.orderByAsc(FlashPromotionSession::getStartTime);
        List<FlashPromotionSession> sessionList = sessionMapper.selectList(lqw);
        return sessionList;
    }

    //添加场次
    @Override
    public void create(FlashPromotionSession promotionSession) {
        sessionMapper.insert(promotionSession);
    }

    //获取全部可选场次及其数量
    @Override
    public List<SmsFlashPromotionSessionVo> selectList(Integer flashPromotionId) {

        List<SmsFlashPromotionSessionVo> voList = new ArrayList<>();
        //建立集合，封装时间段id
        //获取时间段对象
        LambdaQueryWrapper<FlashPromotionSession> lqw = new LambdaQueryWrapper<>();
        lqw.orderByAsc(FlashPromotionSession::getStartTime);
        List<FlashPromotionSession> sessionList = sessionMapper.selectList(lqw);
        //遍历对象集合
        for (FlashPromotionSession session : sessionList) {
            SmsFlashPromotionSessionVo vo = new SmsFlashPromotionSessionVo();
            vo.setId(session.getId());
            vo.setCreateTime(session.getCreateTime());
            vo.setEndTime(session.getEndTime());
            vo.setName(session.getName());
            vo.setStartTime(session.getStartTime());
            vo.setStatus(session.getStatus());
            voList.add(vo);
        }
        //查询SmsFlashPromotionProductRelation表，在活动id相同，时间段id相同，条件下
        for (SmsFlashPromotionSessionVo vo : voList) {
            LambdaQueryWrapper<FlashPromotionProductRelation> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(FlashPromotionProductRelation::getFlashPromotionId, flashPromotionId)
                    .eq(FlashPromotionProductRelation::getFlashPromotionSessionId, vo.getId());
            List<FlashPromotionProductRelation> list = relationMapper.selectList(lqw1);
            int count = list.size();
            vo.setProductCount(count);
        }
        return voList;
    }

    //根据id获取场次详情
    @Override
    public FlashPromotionSession selectById(Integer id) {
        FlashPromotionSession smsFlashPromotionSession = sessionMapper.selectById(id);
        return smsFlashPromotionSession;
    }

    //删除场次
    @Override
    public void delete(Integer id) {
        LambdaQueryWrapper<FlashPromotionSession> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(FlashPromotionSession::getId, id);
        sessionMapper.delete(lqw1);
        LambdaQueryWrapper<FlashPromotionProductRelation> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(FlashPromotionProductRelation::getFlashPromotionSessionId, id);
        relationMapper.delete(lqw2);
    }

    //修改启用状态
    @Override
    public void updateStatus(Integer id, Integer status) {
        LambdaQueryWrapper<FlashPromotionSession> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FlashPromotionSession::getId, id);
        FlashPromotionSession smsFlashPromotionSession = sessionMapper.selectOne(lqw);
        smsFlashPromotionSession.setStatus(status);
        sessionMapper.updateById(smsFlashPromotionSession);
    }

    //修改场次
    @Override
    public void updateById(FlashPromotionSession promotionSession) {
        LambdaQueryWrapper<FlashPromotionSession> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FlashPromotionSession::getId, promotionSession.getId());
        sessionMapper.update(promotionSession, lqw);
    }
}
